package com.chookss.tiku

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.chookss.tiku.entity.LeftEntiry
import com.chookss.tiku.entity.LiveResult
import com.chookss.tiku.entity.RightEntiry
import com.chookss.tiku.response.GetExamInfoResponse
import com.chookss.tools.httpForm
import com.johnrambo.ktea.base.BaseViewModel
import com.johnrambo.ktea.net.http.Method
import com.johnrambo.ktea.net.http.httpBaseList

class SelectSimulationModel : BaseViewModel() {

    val leftLiveData by lazy { MutableLiveData<List<LeftEntiry>>() }
    val rightLiveData by lazy { MutableLiveData<List<LeftEntiry>>() }
    val rightCyLiveData by lazy { MutableLiveData<List<LeftEntiry>>() }
    val errorLiveData by lazy { MutableLiveData<String>() }

    fun getLeft(
            catalogType: String,
            timestamp: String,
            nonce: String,
            sign: String
    ) {
        jobs + httpBaseList<LeftEntiry> {
            url = "api/subject/getCatalog"
            method = Method.POST
            params = mutableMapOf(
                    "catalogType" to catalogType,
                    "timestamp" to timestamp,
                    "nonce" to nonce,
                    "lvlNo" to "1",
                    "sign" to sign
            )
            onSuccess = { leftLiveData.value = it }
            onError = { errorLiveData.value = it }
        }
    }

    fun getRight(
            catalogCode: String,
            timestamp: String,
            nonce: String,
            sign: String
    ) {
        jobs + httpBaseList<LeftEntiry> {
            url = "api/exam/paper/getAnalogPaperByCatalog"
            method = Method.POST
            params = mutableMapOf(
                    "catalogCode" to catalogCode,
                    "timestamp" to timestamp,
                    "nonce" to nonce,
                    "sign" to sign
            )
            onSuccess = { rightLiveData.value = it }
            onError = { errorLiveData.value = it }
        }
    }

    fun getCyRight(
            timestamp: String,
            nonce: String,
            sign: String
    ) {
        jobs + httpBaseList<LeftEntiry> {
            url = "api/exam/paper/getCommonAnalogPaper"
            method = Method.POST
            params = mutableMapOf(
                    "timestamp" to timestamp,
                    "nonce" to nonce,
                    "sign" to sign
            )
            onSuccess = { rightCyLiveData.value = it }
            onError = { errorLiveData.value = it }
        }
    }


    //    /api/exam/getExamInfo
    fun getExamInfo(examCode: String): LiveData<LiveResult<RightEntiry>> {
        var liveResult = MutableLiveData<LiveResult<RightEntiry>>()
        jobs + httpForm<GetExamInfoResponse> {
            url = "api/exam/getExamInfo"
            params = mutableMapOf("examCode" to examCode)
            onSuccess = {
                liveResult.postValue(LiveResult(it.data))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }

}